package com.example.weblending.controlller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.weblending.domain.ApplyNum;
import com.example.weblending.domain.ApplyUnset;
import com.example.weblending.domain.Task;
import com.example.weblending.domain.User;
import com.example.weblending.exception.GlobalException;
import com.example.weblending.service.ApplyNumService;
import com.example.weblending.service.ApplyUnsetService;
import com.example.weblending.service.TaskService;
import com.example.weblending.service.UserService;
import com.example.weblending.utils.JwtUtils;
import com.example.weblending.utils.Result;
import com.example.weblending.vo.UnsetPassword;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/notify")
@CrossOrigin
public class NotifyController {
    @Resource
    private TaskService taskService;
    @Resource
    private UserService userService;

    @Resource
    private ApplyUnsetService applyUnsetService;

    @Resource
    private ApplyNumService applyNumService;

    /**
     * 获得未显示的已经完成的任务信息
     */
    @GetMapping("list")
    public Result list(HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        QueryWrapper<Task> wrapper=new QueryWrapper<>();
        wrapper.eq("oid",user.getUid());
        wrapper.eq("state",1);
        List<Task> list=taskService.list(wrapper);
        wrapper=new QueryWrapper<>();
        wrapper.eq("oid",user.getUid());
        wrapper.eq("state",-1);
        list.addAll(taskService.list(wrapper));
        list.forEach((i)->{
            taskService.removeById(i);
        });
        return Result.ok().data("list",list);
    }

    /**
     * 获得首页面板的数据
     * 自身的状态
     * 未读的消息
     * 管理员 额外获得次数申请数据，重置密码申请数据
     */
    @GetMapping("info")
    public Result info(HttpServletRequest request){
        User user=JwtUtils.checkUser(request,userService);
        Result result= Result.ok();
        if(user.getPermission()!=0){
            result.data("unset",applyUnsetService.getNum(user,false));
            result.data("unsetHistory",applyUnsetService.getNum(user,true));
            if(user.getPermission()==1){
                result.data("num",applyNumService.getNum(user,false));
                result.data("numHistory",applyNumService.getNum(user,true));
            }
        }
        QueryWrapper<Task> wrapper=new QueryWrapper<>();
        wrapper.eq("oid",user.getUid());
        wrapper.eq("state",1);
        List<Task> list=taskService.list(wrapper);
        wrapper=new QueryWrapper<>();
        wrapper.eq("oid",user.getUid());
        wrapper.eq("state",-1);
        list.addAll(taskService.list(wrapper));
        list.forEach((i)->{
            taskService.removeById(i);
        });
        result.data("notify",list);
        return result;
    }
}
